//
//  CGQueryingViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/3.
//  Copyright © 2015年 mac. All rights reserved.
//
/*
 {
 "id": 1,
 "saveTime": "Nov 11, 2015 10:09:50 AM",
 "loaction": "南宁",
 "updateTime": "Nov 26, 2015 10:09:54 AM"
 }
 */

#import "CGQueryingViewController.h"

@interface CGQueryingViewController ()
@property(nonatomic,strong)NSMutableArray *files;

@end

@implementation CGQueryingViewController

-(NSMutableArray *)files{
    if (!_files) {
        _files = [NSMutableArray array];
    }
    return _files;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"档案查询";
    [self initTitleLabel];
    [self initTableView];
    
    // 添加下拉刷新控件
    [self.tableView addHeaderWithTarget:self action:@selector(loadNewStatus)];
    
    // 自动下拉刷新
    [self.tableView headerBeginRefreshing];
    
    //使cell的线条充满
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}

-(void)initTitleLabel{
    
    CGRect aFrame = CGRectMake(0, 0, SCREEN_WIDTH/3, 44);
    UILabel *saveLable = [[UILabel alloc]initWithFrame:aFrame];
    saveLable.text = @"存放时间";
    saveLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:saveLable];
    
    aFrame.origin.x = CGRectGetMaxX(saveLable.frame);
    UILabel *localLabel = [[UILabel alloc]initWithFrame:aFrame];
    localLabel.text = @"档案位置";
    localLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:localLabel];
    
    aFrame.origin.x = CGRectGetMaxX(localLabel.frame);
    UILabel *updayLable = [[UILabel alloc]initWithFrame:aFrame];
    updayLable.text = @"更新时间";
    updayLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:updayLable];
}



#pragma mark 添加表格
-(void)initTableView{
    
    CGRect aRect = CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-64-44);
    self.tableView = [[UITableView alloc] initWithFrame:aRect  style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview: self.tableView];
}

#pragma mark 下拉刷新
-(void)loadNewStatus{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pageSize"] = [NSString stringWithFormat:@"%d",(int)SCREEN_HEIGHT/100];
    parameters[@"userId"] = [CGAccountDefault shareAccount].user_id;
    
    if (!self.files.count) {
        parameters[@"page"] = @"0";   //第一次加载最新的数据
    }else{
        CGFileQuery *fileQuery = [self.files firstObject];
        parameters[@"id"] = [NSString stringWithFormat:@"%d",fileQuery.file_id];
    }
    
    //检查网络是否可用
    if (![ZYHRequest NetWorkIsOK]) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络不可用"];
        // 结束下拉刷新
        [self.tableView headerEndRefreshing];
        return;
    }
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/main/getFilesQuery.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"档案查询请求成功!!");
        // 结束下拉刷新
        [self.tableView headerEndRefreshing];
        //把接收到的数组对象变成model
        
        NSDictionary *jsonDic =  [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        NSString *value = jsonDic[@"data"];
        NSLog(@"value:%@",value);
        
        if (![value isEqualToString:@"null"]) {
            NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
            NSArray *fileArr = [CGFileQuery objectArrayWithJSONData:valueData];
            // 把最新的招聘信息插入到最前面
            NSIndexSet *fileIndex = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, fileArr.count)];
            [self.files insertObjects:fileArr atIndexes:fileIndex];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        return;
    }];
}






#pragma mark overide 表格的代理方法实现
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.files.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"myCell";
    
    CGFileQueryCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[CGFileQueryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //给单元格属性赋值
    cell.fileQuery = self.files[indexPath.row];
    return cell;
    
}

//设置单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

//使cell的线条充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
